package com.cmcc.driver.ui.profile

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.cmcc.driver.R
import com.cmcc.driver.config.DriverPath
import com.cmcc.driver.databinding.ActivityDriverProfileBinding
import com.cmcc.driver.ui.base.BasicActivity
import com.cmcc.driver.ui.dialog.AppAlertDialog
import com.cmcc.driver.ui.profile.viewModel.ProfileViewModel
import com.cmcc.shared.utils.SpHelper
import com.cmcc.shared.utils.clickWithTrigger
import skin.support.SkinCompatManager

@Route(path = DriverPath.DRIVER_PROFILE)
class ProfileActivity : BasicActivity() {
    private lateinit var binding: ActivityDriverProfileBinding

    private lateinit var viewModel: ProfileViewModel

    override fun initialize(savedInstanceState: Bundle?) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_driver_profile)
        binding.appbar.title.text = getString(R.string.profile_title)
        binding.appbar.toolbar.setNavigationOnClickListener { finish() }

        binding.menuTitle.content.clickWithTrigger {
            ARouter.getInstance().build(DriverPath.TAXI_INFORMATION).navigation()
        }

        viewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)

        binding.feedback.menuName.text = getString(R.string.feedback_item)
        binding.feedback.menuIcon.setImageResource(R.drawable.ic_message)
        binding.feedback.clickListener = View.OnClickListener {
            ARouter.getInstance().build(DriverPath.FEED_BACK).navigation()
        }

        binding.articlesOfLaw.menuName.text = getString(R.string.article_of_law_item)
        binding.articlesOfLaw.menuIcon.setImageResource(R.drawable.ic_book)
        binding.articlesOfLaw.clickListener = View.OnClickListener {
            ARouter.getInstance().build(DriverPath.ARTICLES_OF_LAW).navigation()
        }


        binding.sysMsg.menuName.text = getString(R.string.sys_msg)
        binding.sysMsg.menuIcon.setImageResource(R.drawable.ic_setting)
        binding.sysMsg.clickListener = View.OnClickListener {
            //跳转
            ARouter.getInstance().build(DriverPath.MSG_LIST).navigation()
        }

        binding.blackAndWhite.menuName.text = getString(R.string.black_and_white)


        //获取当前的模式

        binding.blackAndWhite.clickListener = View.OnClickListener {

            var type = SpHelper.getParam(this, "Mode", "type", "1");

            AppAlertDialog(this).setContent(
                getString(
                    R.string.switch_day_style,
                    if (type == "1") getString(R.string.white_day) else getString(R.string.black_and_white)
                )
            )
                .setNavigation(getString(R.string.cancel), View.OnClickListener { finish() })
                .setPositive(getString(
                    R.string.need
                ),
                    View.OnClickListener {
                        //当前是夜间模式 设置成白天模式
                        if (type.equals("1")) {
                            //
                            SkinCompatManager.getInstance().loadSkin(
                                "light.skin",
                                SkinCompatManager.SKIN_LOADER_STRATEGY_ASSETS
                            );
                            SpHelper.setParam(this, "Mode", "type", "2");
                        }
                        //当前是白天模式 设置恢复成夜间模式
                        else {
                            SkinCompatManager.getInstance().restoreDefaultTheme()
                            SpHelper.setParam(this, "Mode", "type", "1");
                        }
                    }).show()
        }



        binding.contactUs.contact.clickWithTrigger {
            AppAlertDialog(this).setContent(getString(R.string.if_you_need_call))
                .setNavigation(getString(R.string.cancel), null)
                .setPositive(getString(R.string.make_sure),
                    View.OnClickListener {
                        val data = Uri.parse("tel:85262325253")
                        val intent = Intent(Intent.ACTION_DIAL, data)
                        startActivity(intent)
                    }).show()
        }

        viewModel.getDriverInfo()
        viewModel.requestCarInfo()
    }

    override fun initObserver(savedInstanceState: Bundle?) {
        viewModel.driverInfo.observe(this, Observer { driverInfo ->
            if (driverInfo?.headPortraitAddress != null) {
                binding.avatarUrl = driverInfo.headPortraitAddress
            }

            binding.menuTitle.tvName.text = driverInfo.name
            binding.menuAccount.cardNumber.text = driverInfo.phone
        })

        viewModel.carInfo.observe(this, Observer { car ->
            binding.menuTitle.tvCarCode.text = car.idCard
        })
    }


    override fun onResume() {
        viewModel.getDriverInfo()
        viewModel.requestCarInfo()

        super.onResume()
    }
}